﻿//using System;
//using System.Text;
//using System.Collections.Generic;
//using System.Linq;
//using Microsoft.VisualStudio.TestTools.UnitTesting;

//using Core;
//using FormLogic;
//using ServerAccess;

//using Rhino.Mocks;
//using Rhino.Mocks.Interfaces;
//using Rhino.Mocks.Constraints;

//namespace FormLogic.Tests
//{
//    [TestClass]
//    public class EditUserMarkerPresenterTests
//    {
//        #region Private Members

//        private MockRepository mocks;
//        private ISnpService snpService;
//        private IEditUserMarkerView view;
//        private TestLoginGuard guard;

//        #endregion

//        #region Setup

//        [TestInitialize]
//        public void Setup()
//        {
//            this.mocks = new MockRepository();
//            this.snpService = this.mocks.DynamicMock<ISnpService>();
//            this.view = this.mocks.DynamicMock<IEditUserMarkerView>();
//            this.guard = new TestLoginGuard();
//        }

//        #endregion

//        #region Constructor Tests

//        [TestMethod]
//        public void ConstructorTest()
//        {
//            var snpService = this.mocks.StrictMock<ISnpService>();
//            ILoginRequiredGuard loginGuard = new TestLoginGuard();
//            var view = this.mocks.StrictMock<IEditUserMarkerView>();

//            using (this.mocks.Record())
//            {
//                //presenter should register for events and setup initial view state
//                view.EditEnded += null;
//                LastCall.IgnoreArguments();

//                view.EditingRsNumberChanged += null;
//                LastCall.IgnoreArguments();

//                view.EndEditEnabled = false;
//                view.EditingRsNumber = true;
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(view, loginGuard, snpService);
//            }
//        }

//        #endregion

//        #region Public Method Tests

//        #region LoadMarker

//        [TestMethod]
//        public void LoadMarkerByNameTestValid()
//        {
//            using (this.mocks.Record())
//            {
//                this.view.Marker = new SnpMarkerInfo() { MarkerName = "markerName" };
//                this.view.EndEditEnabled = true;
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);
//                presenter.LoadMarker("markerName");
//                //test passes if the presenter creates a new SnpMarkerInfo and sets the marker property on the view
//            }
//        }

//        [TestMethod]
//        public void LoadMarkerByValueValid()
//        {
//            SnpMarkerInfo marker = new SnpMarkerInfo() { MarkerName = "markerName", Position = "10454525", Chromosome = "10", RsNumber = "1213423" };

//            using (this.mocks.Record())
//            {
//                this.view.Marker = marker;
//                this.view.EndEditEnabled = true;
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);
//                presenter.LoadMarker(marker);
//            }
//        }

//        [TestMethod]
//        public void LoadMarkerByValueNull()
//        {
//            using (this.mocks.Record())
//            {
//                this.view.Marker = null;
//                this.view.EndEditEnabled = false;
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);
//                presenter.LoadMarker((SnpMarkerInfo)null);
//            }
//        }

//        #endregion

//        #endregion

//        #region Event Tests

//        #region EditEnded

//        [TestMethod]
//        public void EditEndedValidRsNumberPresent()
//        {
//            IEventRaiser eventRaiser;

//            using (this.mocks.Record())
//            {
//                this.view.EditEnded += null;
//                LastCall.IgnoreArguments();
//                eventRaiser = LastCall.GetEventRaiser();

//                Expect.Call(this.view.EditingRsNumber).Return(true);
//                Expect.Call(this.view.Marker).Return(new SnpMarkerInfo() { MarkerName = "markerName", RsNumber = "12345" });

//                string errorMessage;
//                int chromosome;
//                long position;
//                Expect.Call(this.snpService.SetUserMarker("markerName", 12345, out chromosome, out position, out errorMessage)).Return(true).OutRef(3, 123456L, String.Empty);

//                //presenter should clear validation errors, set the service message, update the view 'model' with the chromosome and position info
//                this.view.ValidationErrors = null;
//                this.view.ServiceMessage = "Details for markerName set successfully";
//                this.view.Marker = new SnpMarkerInfo() { MarkerName = "markerName", RsNumber = "12345", Chromosome = "3", Position = "123456" };
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);

//                bool updatedEventRaised = false;
//                presenter.MarkerUpdated += (o, e) => { updatedEventRaised = true; };

//                //raise the edit ended event
//                eventRaiser.Raise(null, null);

//                Assert.IsTrue(updatedEventRaised);
//                Assert.IsTrue(this.guard.WasCalled);
//            }
//        }

//        [TestMethod]
//        public void EditEndedValidRsNumberNotValid()
//        {
//            IEventRaiser eventRaiser;

//            using (this.mocks.Record())
//            {
//                this.view.EditEnded += null;
//                LastCall.IgnoreArguments();
//                eventRaiser = LastCall.GetEventRaiser();

//                this.view.ServiceMessage = null;

//                Expect.Call(this.view.EditingRsNumber).Return(true);

//                //marker will fail validation - rs number is not numeric
//                Expect.Call(this.view.Marker).Return(new SnpMarkerInfo() { MarkerName = "markerName", RsNumber = "abc" });

//                //presenter should pass errors to view
//                this.view.ValidationErrors = null;
//                LastCall.IgnoreArguments().Constraints(Is.NotNull());
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);

//                //raise the edit ended event
//                eventRaiser.Raise(null, null);
//            }
//        }

//        [TestMethod]
//        public void EditEndedValidChromosomeAndPosition()
//        {
//            IEventRaiser eventRaiser;

//            using (this.mocks.Record())
//            {
//                this.view.EditEnded += null;
//                LastCall.IgnoreArguments();
//                eventRaiser = LastCall.GetEventRaiser();

//                Expect.Call(this.view.EditingRsNumber).Return(false);

//                SnpMarkerInfo markerInfo = new SnpMarkerInfo() { MarkerName = "markerName", Chromosome = "2", Position = "12344" };
//                Expect.Call(this.view.Marker).Return(markerInfo);

//                string errorMessage;
//                Expect.Call(this.snpService.SetUserMarker("markerName", 2, 12344, out errorMessage)).Return(true).OutRef(String.Empty);

//                this.view.ValidationErrors = null;
//                this.view.ServiceMessage = "Details for markerName set successfully";
//                this.view.Marker = markerInfo;
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);

//                bool updatedEventRaised = false;
//                presenter.MarkerUpdated += (o, e) => { updatedEventRaised = true; };

//                //raise the edit ended event
//                eventRaiser.Raise(null, null);

//                Assert.IsTrue(updatedEventRaised);
//                Assert.IsTrue(this.guard.WasCalled);
//            }
//        }

//        [TestMethod]
//        public void EditEndedValidSnpServiceFailsRsNumberNotFound()
//        {
//            IEventRaiser eventRaiser;

//            using (this.mocks.Record())
//            {
//                this.view.EditEnded += null;
//                LastCall.IgnoreArguments();
//                eventRaiser = LastCall.GetEventRaiser();

//                Expect.Call(this.view.EditingRsNumber).Return(true);
//                Expect.Call(this.view.Marker).Return(new SnpMarkerInfo() { MarkerName = "markerName", RsNumber = "3435343" });

//                int chromosome;
//                long position;
//                string errorMessage;
//                Expect.Call(this.snpService.SetUserMarker("markerName", 3435343, out chromosome, out position, out errorMessage)).Return(false).OutRef(0, 0L, "RS3435343 not found");

//                //no validation errors, but service message should indicate the error
//                this.view.ValidationErrors = null;
//                this.view.ServiceMessage = "Unable to set SNP information: RS3435343 not found";
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);

//                //raise the edit ended event
//                eventRaiser.Raise(null, null);
//                Assert.IsTrue(this.guard.WasCalled);
//            }
//        }

//        [TestMethod]
//        public void EditEndedInvalidChromosomeIndexOutOfRange()
//        {
//            IEventRaiser eventRaiser;

//            using (this.mocks.Record())
//            {
//                this.view.EditEnded += null;
//                LastCall.IgnoreArguments();
//                eventRaiser = LastCall.GetEventRaiser();

//                Expect.Call(this.view.EditingRsNumber).Return(false);

//                //marker will fail validation - chromosome index is out of range
//                Expect.Call(this.view.Marker).Return(new SnpMarkerInfo() { MarkerName = "markerName", Chromosome = "34", Position = "213324345" });

//                //presenter should pass errors to view
//                this.view.ValidationErrors = null;
//                LastCall.IgnoreArguments().Constraints(Is.NotNull());

//                this.view.ServiceMessage = null;
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);

//                //raise the edit ended event
//                eventRaiser.Raise(null, null);
//            }
//        }

//        [TestMethod]
//        public void EditEndedInvalidPositionNotNumeric()
//        {
//            IEventRaiser eventRaiser;

//            using (this.mocks.Record())
//            {
//                this.view.EditEnded += null;
//                LastCall.IgnoreArguments();
//                eventRaiser = LastCall.GetEventRaiser();

//                Expect.Call(this.view.EditingRsNumber).Return(false);

//                //marker will fail validation - position is not numeric
//                Expect.Call(this.view.Marker).Return(new SnpMarkerInfo() { MarkerName = "markerName", Chromosome = "X", Position = "sfds"});

//                //presenter should pass errors to view
//                this.view.ValidationErrors = null;
//                LastCall.IgnoreArguments().Constraints(Is.NotNull());

//                this.view.ServiceMessage = null;
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);

//                //raise the edit ended event
//                eventRaiser.Raise(null, null);
//            }
//        }

//        #endregion

//        #region EditingRsNumberChanged

//        [TestMethod]
//        public void EditingRsNumberChangedTrue()
//        {
//            IEventRaiser eventRaiser;

//            using (this.mocks.Record())
//            {
//                this.view.EditingRsNumberChanged += null;
//                LastCall.IgnoreArguments();
//                eventRaiser = LastCall.GetEventRaiser();

//                Expect.Call(this.view.EditingRsNumber).Return(true);

//                //rs number field should be enabled, the other two should be disabled
//                this.view.IsRsNumberEnabled = true;
//                this.view.IsChromosomeEnabled = false;
//                this.view.IsPositionEnabled = false;
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);

//                //raise event
//                eventRaiser.Raise(null, null);
//            }
//        }

//        [TestMethod]
//        public void EditingRsNumberChangedFalse()
//        {
//            IEventRaiser eventRaiser;

//            using (this.mocks.Record())
//            {
//                this.view.EditingRsNumberChanged += null;
//                LastCall.IgnoreArguments();
//                eventRaiser = LastCall.GetEventRaiser();

//                Expect.Call(this.view.EditingRsNumber).Return(false);

//                //rs number field should be enabled, the other two should be disabled
//                this.view.IsRsNumberEnabled = false;
//                this.view.IsChromosomeEnabled = true;
//                this.view.IsPositionEnabled = true;
//            }

//            using (this.mocks.Playback())
//            {
//                EditUserMarkerPresenter presenter = new EditUserMarkerPresenter(this.view, this.guard, this.snpService);

//                //raise event
//                eventRaiser.Raise(null, null);
//            }
//        }

//        #endregion

//        #endregion
//    }
//}
